<!-- 加载更多 -->
<script setup>
import { ref } from "vue";
import { NSpin } from "naive-ui";
import { useIntersectionObserver } from "@vueuse/core";

const emit = defineEmits(["onloading"]);
let props = defineProps({
  loading: {
    type: Boolean,
    default: false,
  },
  finished: {
    type: Boolean,
    default: false,
  },
});

const container = ref(null);
useIntersectionObserver(
  container,
  ([{ isIntersecting }]) => {
    if (isIntersecting) {
      if (props.loading === false && props.finished === false) {
        emit("onloading");
      }
    }
  },
  {
    threshold: 0,
  }
);
</script>

<template>
  <div class="infinite" ref="container">
    <div class="loading" v-if="props.loading">
      <n-spin size="small" />
      <span class="text">正在加载...</span>
    </div>
    <div class="none" v-if="props.finished">
      <span class="text">亲，没有更多了</span>
    </div>
  </div>
</template>

<style lang='less' scoped>
.infinite {
  width: 100%;
  height: 50px;
  line-height: 50px;
  .loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 50px;

    .text {
      padding-left: 20px;
      color: #999;
      font-size: 15px;
    }
  }
  .none {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 50px;

    .text {
      color: #999;
      font-size: 15px;
    }
  }
}
</style>